package com.gzy.literature.controller;

import com.github.pagehelper.PageInfo;
import com.gzy.literature.entity.WorkCategories;
import com.gzy.literature.service.WorkCategoriesService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;


@RestController
@RequestMapping("workCategories")
public class WorkCategoriesController extends HttpServlet {
    @Resource
    private WorkCategoriesService workCategoriesService;

    @GetMapping("page")
    public ResponseEntity<PageInfo<WorkCategories>> queryByPage(
            @RequestParam(required = false) String categoryName,
            @RequestParam(defaultValue = "1") int pageNumber,
            @RequestParam(defaultValue = "10") int pageSize) {
        if (pageNumber < 1 || pageSize <= 0) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
        WorkCategories workCategories = new WorkCategories();
        if (categoryName != null && !categoryName.trim().isEmpty()) {
            workCategories.setCategoryName(categoryName);
        }
        PageInfo<WorkCategories> pageInfo = workCategoriesService.queryGll(workCategories, (pageNumber - 1) * pageSize, pageSize);
        return ResponseEntity.ok(pageInfo);
    }

    @GetMapping("{id}")
    public ResponseEntity<WorkCategories> queryById(@PathVariable("id") Integer id) {
        if (id == null || id <= 0) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
        WorkCategories workCategories = this.workCategoriesService.queryById(id);
        if (workCategories == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
        return ResponseEntity.ok(workCategories);
    }

    @PostMapping
    public ResponseEntity<WorkCategories> add(@RequestBody WorkCategories workCategories) {
        if (workCategories == null || workCategories.getCategoryName() == null || workCategories.getCategoryName().isEmpty()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
        WorkCategories createdWorkCategories = this.workCategoriesService.insert(workCategories);
        return ResponseEntity.ok(createdWorkCategories);
    }

    @PutMapping
    public ResponseEntity<WorkCategories> edit(@RequestBody WorkCategories workCategories) {
        if (workCategories == null || workCategories.getId() == null || workCategories.getId() <= 0 ||
                workCategories.getCategoryName() == null || workCategories.getCategoryName().isEmpty()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
        WorkCategories updatedWorkCategories = this.workCategoriesService.update(workCategories);
        return ResponseEntity.ok(updatedWorkCategories);
    }

    @DeleteMapping("{id}")
    public ResponseEntity<Boolean> deleteById(@PathVariable("id") Integer id) {
        if (id == null || id <= 0) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(false);
        }
        boolean success = this.workCategoriesService.deleteById(id);
        return ResponseEntity.ok(success);
    }
}